package com.itboye.map;

import java.util.ArrayList;
import java.util.List;

import com.itboye.base.BaseActivity;
import com.itboye.map.MyLetterSortView.OnTouchingLetterChangedListener;
import com.itboye.sunsun.application.App;
import com.itboye.sunsun.choosecity.CityListActivity;
import com.itboye.sunsun.constants.SPContants;
import com.itboye.sunsun.utils.SPUtils;
import com.itboye.sunsunhome.R;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

/**
 * 城市列表
 * 
 * @author sy
 * 
 */
public class RegionActivity extends BaseActivity implements OnClickListener {
	public static final String TAG = RegionActivity.class.getSimpleName();

	private Context context = RegionActivity.this;

	private EditText mClearEditText;
	private TextView tv_mid_letter;
	private ListView listView;
	private MyLetterSortView right_letter;

	private ItemBeanAdapter mAdapter;
	private List<LogonCity> mlist = new ArrayList<LogonCity>();
	private InputMethodManager inputMethodManager;

	private View mCityContainer;
	private FrameLayout mSearchContainer;
	private ListView mSearchListView;
	private SearchCityAdapter mSearchCityAdapter;//搜索adapter
	private ImageView close_icon, delete;
	private TextView dangqian, gpsTv;

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);

		setContentView(R.layout.activity_reagion);
		initVieww();
		setLinstener();
		initData();
		fillData();
		setStatusBarColor(R.color.home_blue);
		gpsTv = (TextView) findViewById(R.id.gpsTv);
		gpsTv.setText(SPUtils.get(this, null, SPContants.SELECT_CITY, "") + "定位失败...");
		close_icon = (ImageView) findViewById(R.id.close_icon);
		dangqian = (TextView) findViewById(R.id.dangqian);
		delete=(ImageView) findViewById(R.id.delete);
		final String province = (String) SPUtils.get(this, null,
				SPContants.PROVINCE, "");
		if (province == null) {
			dangqian.setText("当前城市:"+SPUtils
					.get(this, null, SPContants.SELECT_CITY, "") + "");
		} else {
			dangqian.setText("当前城市:"+SPUtils.get(this, null, SPContants.PROVINCE, "")
					+ "");
		}
		delete.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mClearEditText.setText("");
			}
		});
		close_icon.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		dangqian.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (province == null) {
					mClearEditText.setText(SPUtils.get(App.ctx, null,
							SPContants.SELECT_CITY, "") + "");
				} else {
					mClearEditText.setText(province);
				}

			}
		});
	}

	protected void initData() {

		getDataCity();
		mAdapter = new ItemBeanAdapter(this, mlist);
//		listView.setEmptyView(findViewById(R.id.citys_list_load));
		listView.setVisibility(View.GONE);
//		listView.setAdapter(mAdapter);

	}

	protected void initVieww() {

//		inputMethodManager = (InputMethodManager) this
//				.getSystemService(Context.INPUT_METHOD_SERVICE);
		listView = (ListView) findViewById(R.id.list);
		mClearEditText = (EditText) findViewById(R.id.et_msg_search);
		// 这里设置中间字母
		right_letter = (MyLetterSortView) findViewById(R.id.right_letter);
		tv_mid_letter = (TextView) findViewById(R.id.tv_mid_letter);
		right_letter.setTextView(tv_mid_letter);
		// 搜索
		mCityContainer = findViewById(R.id.city_content_container);
		mSearchContainer = (FrameLayout) this
				.findViewById(R.id.search_content_container);
		mSearchListView = (ListView) findViewById(R.id.search_list);
		mSearchListView.setEmptyView(findViewById(R.id.search_empty));
		mSearchContainer.setVisibility(View.GONE);

	}

	protected void setLinstener() {
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent();
				intent.putExtra("number", mlist.get(position).getNumber());
				System.out.println("citycitycity"
						+ mlist.get(position).getNumber());
				intent.putExtra("province", mlist.get(position).getProvince());
				setResult(104, intent);
				finish();

				SPUtils.put(App.ctx, null, SPContants.PROVINCE,
						mlist.get(position).getProvince());
			}
		});

		listView.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// 隐藏软键盘
				if (getWindow().getAttributes().softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) {
					if (getCurrentFocus() != null)
						inputMethodManager.hideSoftInputFromWindow(
								getCurrentFocus().getWindowToken(),
								InputMethodManager.HIDE_NOT_ALWAYS);
				}
				return false;
			}
		});

		// 设置右侧触摸监听
		right_letter
				.setOnTouchingLetterChangedListener(new OnTouchingLetterChangedListener() {

					@Override
					public void onTouchingLetterChanged(String s) {
						// 该字母首次出现的位置
						int position = mAdapter.getPositionForSection(s
								.charAt(0));
						if (position != -1) {
							listView.setSelection(position);
						}

					}
				});

		// 根据输入框输入值的改变来过滤搜索
		mClearEditText.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// 当输入框里面的值为空，更新为原来的列表，否则为过滤数据列表
				
				if(s.length()>0){
					delete.setVisibility(View.VISIBLE);
				}else
				{
					delete.setVisibility(View.GONE);
				}
				
//				Intent intent=new Intent(App.ctx,CityListActivity.class);
//				startActivity(intent);
				filterData2(s.toString());
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {
			}
		});

		mSearchListView
				.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> parent, View view,
							int position, long id) {
						Intent intent = new Intent();
						intent.putExtra("number",
								mSearchCityAdapter.getItem(position)
										.getNumber());
						intent.putExtra("province",
								mSearchCityAdapter.getItem(position)
										.getProvince());
//						SPUtils.put(App.ctx, null, SPContants.PROVINCE,
//								mSearchCityAdapter.getItem(position)
//								.getProvince());
						setResult(104, intent);
						SPUtils.put(App.ctx, null, SPContants.SEWARPROVINCE, mSearchCityAdapter.getItem(position)
								.getProvince()+"市");
						System.out.println("resultDataresultDatasssss"+mSearchCityAdapter.getItem(position)
								.getProvince());
						Intent intent2 = new Intent("resultDataresin");
						sendBroadcast(intent2);
						finish();
					}
				});
	}

	protected void fillData() {
		// TODO Auto-generated method stub

	}

	private void getDataCity() {

		new Thread(new Runnable() {

			@Override
			public void run() {
				MyCityDBHelper myCityDBHelper = new MyCityDBHelper(context);

				mlist = myCityDBHelper.getCityDB().getAllCity();
				mHandler.sendEmptyMessage(0);

			}
		}).start();
	}

	private void filterData2(String filterStr) {
		mSearchCityAdapter = new SearchCityAdapter(RegionActivity.this, mlist);
		mSearchListView.setAdapter(mSearchCityAdapter);
		mSearchListView.setTextFilterEnabled(true);
		if (mlist.size() < 1 || TextUtils.isEmpty(filterStr)) {
			mCityContainer.setVisibility(View.VISIBLE);
			mSearchContainer.setVisibility(View.INVISIBLE);

		} else {

			mCityContainer.setVisibility(View.INVISIBLE);
			mSearchContainer.setVisibility(View.VISIBLE);
			mSearchCityAdapter.getFilter().filter(filterStr);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.back:
			finish();
			break;
		//
		default:
			break;
		}

	}

	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 0:

				mAdapter.updateListView(mlist);
				break;
			default:
				break;
			}
		}
	};
}
